package com.ep.instruktor.harmonogram;

import java.util.Calendar;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.ep.general.GeneralClassMdt;
import com.ep.instruktor.harmonogram.db.HarmonogramKursu;

public class HarmonogramKursuMdt extends GeneralClassMdt<HarmonogramKursu> {

	public HarmonogramKursuMdt() {
		super(HarmonogramKursu.class);
	}

	@SuppressWarnings("unchecked")
	public List<HarmonogramKursu> getHarmonogram(int instruktorId) {
		getSession().beginTransaction();

		Criteria crit = getSession().createCriteria(HarmonogramKursu.class);
		crit.add(Restrictions.eq("instruktor.id", instruktorId));
		crit.addOrder(Order.desc("data"));

		List<HarmonogramKursu> result = crit.list();

		getSession().getTransaction().commit();
		return result;
	}
	
	public List<HarmonogramKursu> getbyMonth(int instruktorId) {
		StringBuilder sb = new StringBuilder();
		sb.append("SELECT * FROM i_d_harmonogram_kursu WHERE MONTH(data) = ");
		sb.append(Calendar.getInstance().get(Calendar.MONTH) + 1);
		sb.append(" AND YEAR(data) = ");
		sb.append(Calendar.getInstance().get(Calendar.YEAR));
		sb.append(" AND d_instruktor_id=");
		sb.append(instruktorId);
		sb.append(" ORDER BY data DESC");

		getSession().beginTransaction();
		SQLQuery query = getSession().createSQLQuery(sb.toString());
		query.addEntity(HarmonogramKursu.class);

		return list(query);
	}

	public List<HarmonogramKursu> getByWeek(int instruktorId) {
		StringBuilder sb = new StringBuilder();
		sb.append("SELECT * FROM i_d_harmonogram_kursu");
		sb.append(" WHERE data >= CURDATE()");
		sb.append(" AND data <= DATE_ADD(CURDATE(), INTERVAL 7 DAY) AND d_instruktor_id=");
		sb.append(instruktorId);
		sb.append(" AND YEAR(data) = ");
		sb.append(Calendar.getInstance().get(Calendar.YEAR));
		sb.append(" ORDER BY data DESC");

		getSession().beginTransaction();
		SQLQuery query = getSession().createSQLQuery(sb.toString());
		query.addEntity(HarmonogramKursu.class);

		return list(query);
	}

}
